package org.smoozik.app.mode.rythmtrainer;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.smoozik.app.ihm.Constants;
import org.smoozik.app.mode.ModeSettings;

public class RythmTrainerSettings implements ModeSettings {
	JPanel mainPanel;
	RythmTrainerMode mode;

	private JTextField speedTextField;
	private JTextField nbTimes;
	private JTextField nbMeasures;

	private JCheckBox cb1;
	private JCheckBox cb2;
	private JCheckBox cb3;
	private JCheckBox cb4;
	private JCheckBox noLiaison;

	public RythmTrainerSettings(RythmTrainerMode mode) {
		this.mode = mode;
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		JPanel parameters = new JPanel();
		parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
		parameters.setBorder(new TitledBorder("Game Parameters"));

		mainPanel.add(parameters);

		speedTextField = new JTextField(5);
		addComponent(parameters, Constants.SPEED_LABEL, speedTextField);

		nbTimes = new JTextField(5);
		addComponent(parameters, "nbTimes", nbTimes);

		nbMeasures = new JTextField(5);
		addComponent(parameters, "nbMeasures", nbMeasures);

		JPanel componentsPanel = new JPanel();
		noLiaison = new JCheckBox();
		addComponent(componentsPanel, "no liaison", noLiaison);
		cb1 = new JCheckBox();
		addComponent(componentsPanel, "double croche", cb1);
		cb2 = new JCheckBox();
		addComponent(componentsPanel, "croche", cb2);
		cb3 = new JCheckBox();
		addComponent(componentsPanel, "noire", cb3);
		cb4 = new JCheckBox();
		addComponent(componentsPanel, "blanche", cb4);
		parameters.add(componentsPanel);
	}

	private void addComponent(JPanel origine, String text, Component component) {
		JPanel panel = new JPanel();
		if (text != null) {
			JLabel label = new JLabel(text);
			panel.add(label);
		}
		panel.add(component);
		origine.add(panel);
	}

	@Override
	public JPanel getMainPanel() {
		return this.mainPanel;
	}

	public void refreshParameters() {
		speedTextField.setText("" + mode.getSongGenerator().getSpeed());
		nbTimes.setText("" + mode.getSongGenerator().getNbTimes());
		nbMeasures.setText("" + mode.getSongGenerator().getNbMeasures());

		double[] rythmes = mode.getSongGenerator().getRythme();

		cb4.setSelected(false);
		cb3.setSelected(false);
		cb2.setSelected(false);
		cb1.setSelected(false);
		for (double d : rythmes) {
			if (d == 2) {
				cb4.setSelected(true);
			}
			if (d == 1) {
				cb3.setSelected(true);
			}
			if (d % 1 == 0.5) {
				cb2.setSelected(true);
			}
			if (d % 1 == 0.25) {
				cb1.setSelected(true);
			}
		}

		if (mode.getSongGenerator().isNoLiaison()) {
			noLiaison.setSelected(true);
		} else {
			noLiaison.setSelected(false);
		}
	}

	@Override
	public void saveParameters() {
		ArrayList<Double> liste = new ArrayList<>();
		if (cb1.isSelected()) {
			liste.add(new Double(0.25));
		}
		if (cb2.isSelected()) {
			liste.add(new Double(0.5));
			if (cb1.isSelected()) {
				liste.add(new Double(0.75));
			}
		}
		if (cb3.isSelected()) {
			liste.add(new Double(1));
			if (cb2.isSelected()) {
				liste.add(new Double(1.5));
				if (cb1.isSelected()) {
					liste.add(new Double(1.25));
					liste.add(new Double(1.75));
				}
			}
		}
		if (cb4.isSelected()) {
			liste.add(new Double(2));
			if (cb3.isSelected()) {
				liste.add(new Double(3));
				if (cb2.isSelected()) {
					liste.add(new Double(2.5));
					liste.add(new Double(3.5));
					if (cb1.isSelected()) {
						liste.add(new Double(2.25));
						liste.add(new Double(2.75));
						liste.add(new Double(3.25));
						liste.add(new Double(3.75));
					}
				}
			}
		}
		double[] rythme = new double[liste.size()];
		int i = 0;
		for (Double d : liste) {
			rythme[i] = d.doubleValue();
			i++;
		}
		mode.getSongGenerator().setRythme(rythme);

		mode.getSongGenerator().setNoLiaison(noLiaison.isSelected());

		// UI
		if (speedTextField.getText() != null
				&& speedTextField.getText().trim().length() != 0) {
			mode.getSongGenerator().setSpeed(
					Integer.parseInt(speedTextField.getText()));
		}
		if (nbMeasures.getText() != null
				&& nbMeasures.getText().trim().length() != 0) {
			mode.getSongGenerator().setNbMeasures(
					Integer.parseInt(nbMeasures.getText()));
		}
		if (nbTimes.getText() != null && nbTimes.getText().trim().length() != 0) {
			mode.getSongGenerator().setNbTimes(
					Integer.parseInt(nbTimes.getText()));
		}
	}

}
